package com.galeno.day06;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 2021/10/2123:31
 */
public class FlinkRestart3 {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest",55555);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        env.enableCheckpointing(5000);
        DataStreamSource<String> lines = env.socketTextStream("192.168.77.3", 9999);
        SingleOutputStreamOperator<Tuple2<String, Integer>> map = lines.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                String[] split = value.split(",");
                String s1 = split[0];
                String s2 = split[1];
                if (s1.contains("error")) {
                    throw new RuntimeException("hey gun your have an exception");
                }
                return Tuple2.of(s1, Integer.parseInt(s2));


            }
        });
        KeyedStream<Tuple2<String, Integer>, String> tuple2StringKeyedStream = map.keyBy(x -> x.f0);
        SingleOutputStreamOperator<Tuple2<String, Integer>> sumed = tuple2StringKeyedStream.sum(1);
        sumed.print();
        env.execute();


    }
}
